﻿using System;
using Guru.Web.UI;
using System.Data;
using Guru.Data;

public partial class UserControl_CheckOut : GuruUserControl
{
    protected int RoomID
    {
        get
        {
            return int.Parse(Page.SecureQueryString["roomID"]);
        }
    }
    protected int InvoiceID
    {
        get
        {
            return int.Parse(Page.SecureQueryString["InvoiceID"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            uxCheckOutDate.SelectedDate = DateTime.Now;            
        }
    }

    public DataTable LoadDetails()
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);       
        DataTable tb = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Invoice_GetDetails", parameters);

        this.uxFromDate.SelectedDate = Convert.ToDateTime(tb.Rows[0]["CheckInDate"].ToString());
        this.uxPrepaid.Value = tb.Rows[0]["Prepaid"].ToMoney();
        this.uxOtherFees.Value = tb.Rows[0]["AddSubTotal"].ToMoney();
        this.uxPromotion.Value = tb.Rows[0]["Promotion"].ToMoney();
        this.uxVAT.Value = tb.Rows[0]["VAT"].ToMoney();
        this.uxDescriptions.Text = tb.Rows[0]["Description"].ToSafeString();

        this.uxRoomAndServicesFees.Value = tb.Rows[0]["TotalFees"].ToMoney();
        double amount = this.uxRoomAndServicesFees.Value.Value + this.uxOtherFees.Value.Value - this.uxPromotion.Value.Value;
        amount = amount + amount * this.uxVAT.Value.Value;
        this.uxAmount.Value = amount;
        this.uxRemaining.Value = amount - this.uxPrepaid.Value.Value;
        return tb;
        
    }
    public void Save()
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);
        parameters.Add("@Prepaid", (this.uxPrepaid.Value == null) ? 0 : this.uxPrepaid.Value.Value, DbType.Currency);
        parameters.Add("@Promotion", (this.uxPromotion.Value == null) ? 0 : this.uxPromotion.Value.Value, DbType.Currency);
        parameters.Add("@Description", this.uxDescriptions.Text, DbType.String);
        parameters.Add("@VAT", (this.uxVAT.Value == null) ? 0 : this.uxVAT.Value.Value, DbType.Double);
        parameters.Add("@AddSubTotal", (this.uxOtherFees.Value == null) ? 0 : this.uxOtherFees.Value.Value, DbType.Currency);
        HSMCommon.HSMBusiness.UpdateData("spa_HSM_Invoice_Save", parameters);
    }
    public void CheckOut()
    {
        this.Save();

        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);       
        parameters.Add("@Status", HSMEnums.RoomStatus.Dirty, DbType.Int16);
        HSMCommon.HSMBusiness.UpdateData("spa_HSM_Invoice_CheckOut", parameters);
    }
}
